package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.enums.BillTypeEn;
import com.esyoga.yoyoga.enums.UserTypeEn;
import com.esyoga.yoyoga.mapper.TBillMapper;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.service.GymService;
import com.esyoga.yoyoga.service.PayService;
import com.esyoga.yoyoga.service.StudentService;
import com.esyoga.yoyoga.service.TrainerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service("payService")
@Transactional
public class PayServiceImpl implements PayService {
    @Resource
    TBillExample billExample;
    @Resource
    TBillMapper billMapper;
    @Resource
    StudentService studentService;
    @Resource
    TrainerService trainerService;
    @Resource
    GymService gymService;


    @Override
    public int insertBillRecord(Integer uId, UserTypeEn userType, BillTypeEn billType, String out_trade_no, String trade_no) {
        TBill bill = new TBill();
        bill.setbUserid(uId);
        bill.setbUsertype(userType.getCode());
        bill.setbBilltype(billType.getCode());
        bill.setbOrderTradeNo(out_trade_no);
        bill.setbTradeNo(trade_no);
        //todo ：差一个账单金额
        return billMapper.insertSelective(bill);
    }

    @Override
    public void updateBalanceAndInsertBillRecord(BillTypeEn billType, String out_trade_no, String trade_no, BigDecimal money, Integer uId, UserTypeEn userType) {
            if(userType==null||uId==null){
                return ;
            }

            switch (userType){
                case STUDENT:
                    //增加充值记录
                    insertBillRecord(uId,userType,billType,out_trade_no,trade_no);
                    //更新钱包余额
                    studentService.updateStudentBalance(money,uId);
                    break;
                case TRAINER:
                    insertBillRecord(uId,userType,billType,out_trade_no,trade_no);
                    trainerService.updateTrainerBalance(money,uId);
                    break;
                case GYM:
                    insertBillRecord(uId,userType,billType,out_trade_no,trade_no);
                    gymService.updateTGymBalance(money,uId);
                    break;
                default:

            }
    }

    @Override
    public List<TBill> selectBillByUser(Integer uId, Integer userType) {
        billExample.clear();
        TBillExample.Criteria criteria = billExample.createCriteria();
        criteria.andBUseridEqualTo(uId).andBUsertypeEqualTo(userType);
        return billMapper.selectByExample(billExample);
    }
}
